package com.limh.gobang.activity

import android.content.Intent
import android.os.Handler
import android.text.TextUtils
import android.util.Log
import com.limh.gobang.R
import com.limh.gobang.base.BaseActivity
import com.limh.gobang.base.Consts
import com.limh.gobang.bean.User
import com.limh.gobang.utils.GsonUtils
import com.limh.gobang.utils.SpUtils
import kotlinx.android.synthetic.main.activity_login.*

/**
 * @function 登陆页面
 * @author limh
 * @date 2018/5/3 17:11
 */
class LoginActivity : BaseActivity() {

    override fun findView() {
        setContentView(R.layout.activity_login)
    }

    override fun initViews() {
        btnLogin.setOnClickListener { login() }
    }

    override fun initDatas() {
    }

    private fun login() {
        if (TextUtils.isEmpty(editName.text.toString()) || TextUtils.isEmpty(editPass.text.toString())) {
            showToast("用户名或密码不能为空")
            return
        }
        Handler().postDelayed({
            val name = editName.text.toString()
            val pass = editPass.text.toString()
            val user = User()
            user.username = name
            user.password = pass
            user.userbgId = (Math.random() * 5).toInt()
            user.grade = "大师"
            user.win = 0
            user.total = 0
            SpUtils.put(this, Consts.SP_USER, GsonUtils.bean2Str(user))
            Log.d(TAG, "保存结果=${SpUtils.get(this, Consts.SP_USER, "") as String}")
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 2000)
    }
}